package class03;

import java.util.*;

/**
 * <a href="https://leetcode.cn/problems/implement-queue-using-stacks/">232. 用栈实现队列</a>
 *
 * 面试小细节：如果面试官问，如何用栈实现图的广度优先遍历，可以用两个栈模拟队列来实现
 */
public class Code06_TwoStacksImplementQueue {

    public static class MyQueue {
        public Stack<Integer> pushStack, popStack;

        public MyQueue() {
            pushStack = new Stack<>();
            popStack = new Stack<>();
        }

        public void push(int x) {
            pushStack.push(x);
        }

        public int pop() {
            pushToPop();
            return popStack.pop();
        }

        public int peek() {
            pushToPop();
            return popStack.peek();
        }

        public boolean empty() {
            return pushStack.isEmpty() && popStack.isEmpty();
        }

        public void pushToPop() {
            if (popStack.isEmpty()) {
                while (!pushStack.isEmpty()) {
                    popStack.push(pushStack.pop());
                }
            }
        }
    }
}
